package com.yfq.test;


import com.yfq.springframework.beans.BeansException;
import com.yfq.springframework.core.convert.converter.Converter;

import java.beans.Beans;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author derekyi
 * @date 2021/1/17
 */
public class StringToLocalDateConverter implements Converter<String, Date> {
    private final SimpleDateFormat SIMPLE_DATE_FORMAT;

    public StringToLocalDateConverter(String pattern) {
        SIMPLE_DATE_FORMAT = new SimpleDateFormat(pattern);
    }

    @Override
    public Date convert(String source) {
        try {
            return SIMPLE_DATE_FORMAT.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BeansException("错误的类型转换：" + source + "," + e.getMessage());
        }
    }

}
